﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UserDefineForm.control;

namespace UserDefineForm.formcomponent
{
    public partial class PropertiesPanel : UserControl
    {
        private DesignableControlBase control;
        private DesignPanel designPnl;

        private Font tempFont;
        private List<string> listValue;

        public PropertiesPanel(DesignPanel desPnl)
        {
            this.designPnl = desPnl;
            InitializeComponent();
            addTextChangeEventHandler();

        }

        private void addTextChangeEventHandler()
        {
            nameTxt.TextChanged += new EventHandler(textChanged);
            topTxt.TextChanged += new EventHandler(textChanged);
            leftTxt.TextChanged += new EventHandler(textChanged);
            widthTxt.TextChanged += new EventHandler(textChanged);
            heightTxt.TextChanged += new EventHandler(textChanged);
            fontTxt.TextChanged += new EventHandler(textChanged);
            defaultTxt.TextChanged += new EventHandler(textChanged);
        }

        void textChanged(object sender, EventArgs e)
        {
            applyBtn.Enabled = true;
        }

        public void setControl(DesignableControlBase control)
        {
            if (control == null)
            {
                clear();
                return;
            }
            this.Enabled = true;
            
            this.control = control;
            this.nameTxt.Text = control.Name;
            this.topTxt.Text = control.Location.Y.ToString();
            this.leftTxt.Text = control.Location.X.ToString();
            this.widthTxt.Text = control.Size.Width.ToString();
            this.heightTxt.Text = control.Size.Height.ToString();
            this.fontTxt.Text = control.FontProp.FontFamily.Name + ", " + control.FontProp.Size.ToString();
            this.fontTxt.Font = new Font(control.FontProp.FontFamily,this.fontTxt.Font.Size, control.FontProp.Style) ;
            tempFont = control.Font;
            this.defaultTxt.Text = control.DefaultValue;

            if (!(control is DesignableComboBox))
            {
                listPnl.Enabled = false;
            }
            else
            {
                listPnl.Enabled = true;
                listValue = ((DesignableComboBox)control).ListValue;
                setListStringToCmbBox(listComboBox, listValue);
            }
            applyBtn.Enabled = false;
            resetBtn.Enabled = true;
            deleteBtn.Enabled = true;
        }

        private void clear()
        {
            nameTxt.Text = "";
            topTxt.Text = "";
            leftTxt.Text = "";
            widthTxt.Text = "";
            heightTxt.Text = "";
            fontTxt.Text = "";
            defaultTxt.Text = "";
            listComboBox.Text = "";
            applyBtn.Enabled = false;
            resetBtn.Enabled = false;
            deleteBtn.Enabled = false;

            control = null;
            tempFont = null;
            listValue = new List<string>();

        }

        public void refreshInfo()
        {
            setControl(control);
        }

        private void applyProp()
        {
            if (control == null) return;
            try
            {
                int top = int.Parse(topTxt.Text);
                int left = int.Parse(leftTxt.Text);
                int width = int.Parse(widthTxt.Text);
                int height = int.Parse(heightTxt.Text);

                control.Name = this.nameTxt.Text;
                control.Location = new Point(left, top);
                control.Size = new Size(width, height);
                control.FontProp = tempFont;
                control.DefaultValue = defaultTxt.Text;
                control.ListValue = listValue;

                setControl(control);
                designPnl.Refresh();
            }
            catch (Exception e)
            {
                MessageBox.Show("Invalid property value");
            }

        }

        private void fontBtn_Click(object sender, EventArgs e)
        {
            FontDialog fontDlg = new FontDialog();
            fontDlg.Font = control.FontProp;
            if (fontDlg.ShowDialog() != DialogResult.Cancel)
            {
                tempFont = fontDlg.Font;
                this.fontTxt.Text = control.FontProp.FontFamily.Name + ", " + control.FontProp.Size.ToString();
                this.fontTxt.Font = new Font(control.FontProp.FontFamily, this.fontTxt.Font.Size, control.FontProp.Style);
                applyBtn.Enabled = true;
            }
        }

        private void applyBtn_Click(object sender, EventArgs e)
        {
            applyProp();
        }

        private void resetBtn_Click(object sender, EventArgs e)
        {
            refreshInfo();
        }

        private List<string> getListStringFromCmbBox(ComboBox c)
        {
            List<string> list = new List<string>();
            foreach (object o in c.Items)
            {
                list.Add((string)o);
            }
            return list;
        }
        private void setListStringToCmbBox(ComboBox c, List<string> list)
        {
            c.Items.Clear();
            foreach (string s in list)
            {
                c.Items.Add(s);
            }
        }

        private void addListBtn_Click(object sender, EventArgs e)
        {
            string value = listComboBox.Text;
            foreach (object o in listComboBox.Items)
            {
                if (value.Equals(((string)o).Trim()))
                {
                    MessageBox.Show("Existed value in list");
                    return;
                }
            }

            listValue.Add(value);
            listComboBox.Items.Add(value);
            listComboBox.Text = "";
            applyBtn.Enabled = true;
        }

        private void deleteBtn_Click(object sender, EventArgs e)
        {
            designPnl.deleteControl(control);
            setControl(null);
        }
    }
}
